package main

import (
	"fmt"
	"net"
	"encoding/binary"
	"time"
)

func test(i, n int){

	fmt.Println("test ", i)
	// connect server
	conn, err := net.Dial("tcp", "127.0.0.1:8080")
	if err != nil {
		fmt.Println("net.Dial fail,", err)
		return
	}

	now := time.Now()
	data := [1024]byte{}
	for i := 0; i < n; i = i+1 {
		//fmt.Println("please input msg:")
		var str string = "hello world"
		//fmt.Scanln(&str)
		//fmt.Println("input msg:", str)
		
		binary.BigEndian.PutUint16(data[0:2], uint16(len(str) + 2))
		binary.BigEndian.PutUint16(data[2:4], uint16(1))
		copy(data[4:], []byte(str)) 
		
		dataLen := len(str)
		
		wn, err := conn.Write(data[0:dataLen+4])
		if err != nil {
			fmt.Println("conn write failed:", err)
			break
		}
		//conn.SetNoDelay(true)
		if wn != (dataLen + 4) {
			fmt.Println("conn write data not full pkt:", wn, len(data))
			break
		}
		//fmt.Println("conn write data:", wn, len(data))
		
		rn, err := conn.Read(data[0:])
		if rn != dataLen {
			fmt.Println("conn read data:", string(data[0:rn]), rn)
		}
		//fmt.Println("conn read data:", string(data[0:rn]), rn)
		//time.Sleep(time.Millisecond*1)
	}
	
	end := time.Now()
	d := end.Sub(now)
	fmt.Println("test ove", i, d)
}

func main() {
	
	fmt.Println("hello dd")
	for i := 0; i < 200; i = i + 1 {
		go test(i, 10000)
	}
	
	for {
		time.Sleep(1)
	}
}

